extends NodeState


var direction: Vector2
var is_tackle_complete: bool
var tackle_finish_time: float


var player: Player:
	get:
		return state_matchine.root as Player


func _on_process(delta : float) -> void:
	process_animation()
	process_tackling(delta)



func _on_enter(_data: NodeStateData) -> void:
	direction = player.input_manager.movement()
	player.velocity = player.tackling_speed * direction

	is_tackle_complete = false
	tackle_finish_time = player.duration_tackle_to_recover


func _on_exit() -> void:
	pass



func process_animation():
	player.animation_player.play(Player.ANIMATION_TACKLE)


func process_tackling(delta : float):
	if not is_tackle_complete:
		player.velocity = player.velocity.move_toward(Vector2.ZERO, delta * GlobalConstants.PLAYER_GROUND_FRICTION)
		if player.velocity == Vector2.ZERO:
			is_tackle_complete = true
			tackle_finish_time = player.duration_tackle_to_recover

		player.move_and_slide()
		return

	if tackle_finish_time > 0.0:
		tackle_finish_time -= delta
		return
	transition_state(Player.STATE_RECOVERING)
